package net.huashitong.synthetical.newSupervision.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.*;
import com.sirdc.modules.sys.util.SysUserUtils;
import net.huashitong.synthetical.newSupervision.entity.*;
import net.huashitong.synthetical.newSupervision.filter.TaskMessageFilter;
import net.huashitong.synthetical.newSupervision.filter.TaskNewFilter;
import net.huashitong.synthetical.newSupervision.filter.TaskSendFilter;
import net.huashitong.synthetical.newSupervision.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/10
 * Time: 15:29
 * To change this template use File | Settings | File Templates.
 * Description:单位
 */
@Controller
@RequestMapping("/taskSend")
public class TaskSendController extends JsonBaseController {

    @Autowired
    private TaskSendService taskSendService;

    @Autowired
    private TaskNewService taskNewService;

    @Autowired
    private TaskLogService taskLogService;
@Autowired
private SysUserService sysUserService;

    @Autowired
    private TaskCopeToService taskCopeToService;
    @Autowired
    private TaskWorkableService taskWorkableService;

    @Autowired
    private SysUserPermissionService sysUserPermissionService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private TaskBackService taskBackService;

    @Autowired
    private TaskMessageService taskMessageService;
    @Autowired
    private DeptService deptService;

    @Override
    protected String getView(String s) {
        return "/modules/project/taskSend/" + s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    @RequestMapping("/filter")
    public String filter() {
        return getView("filter");
    }

    /**
     * 单位用户任务列表
     *
     * @param taskNewFilter
     * @return
     */

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid queryFilter(TaskNewFilter taskNewFilter) {
        String userId = SysUserUtils.getUserId();
        taskNewFilter.setUserId(userId);
        List<TaskNew> list = taskSendService.queryFilter(taskNewFilter);
        return coverJqGrid(taskNewFilter, list);
    }


    /**
     * 返回签收批转页面
     *
     * @param id TaskSend表的主键
     * @return
     */
    @RequestMapping(value = "/signAndEndorse/{id}", method = RequestMethod.GET)
    public String signAndEndorse(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew task = taskNewService.getById(taskId);
        List<TaskSend> sendList = taskSendService.queryByTaskId(taskId);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(taskId);
        model.addAttribute("taskNew", task);
        model.addAttribute("sendList", sendList);
        model.addAttribute("workableList", workableList);
        model.addAttribute("taskSendId", id);
        return getView("form");
    }

    /**
     * 单位任务签收批转
     *
     * @param taskSendFilter
     */
    @ResponseBody
    @RequestMapping(value = "/signAndEndorse", method = RequestMethod.POST)
    public Message signAndEndorse(TaskSendFilter taskSendFilter) {
        String result = taskSendService.updateSign(taskSendFilter);
        if ("200".equals(result)) {
            return coverMessage("200");
        } else {
            return coverMessage("500", result);
        }
    }

    /**
     * 返回反馈页面  可编辑
     *
     * @param id TaskSend表的主键
     * @return
     */
    @RequestMapping(value = "/feedback/{id}", method = RequestMethod.GET)
    public String feedback(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew task = taskNewService.getById(taskId);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(taskId);
        List<TaskBack> list = taskBackService.getBySendId(id);
        model.addAttribute("list", list);
        model.addAttribute("taskNew", task);
        //model.addAttribute("workableList", workableList);
        model.addAttribute("taskSendId", id);
        return getView("feedback");
    }


    /**
     * 返回反馈页面查看
     *
     * @param id TaskSend表的主键
     * @return
     */
    @RequestMapping(value = "/backList/{id}", method = RequestMethod.GET)
    public String backList(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew task = taskNewService.getById(taskId);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(taskId);
        List<TaskBack> list = taskBackService.getBySendId(id);
        model.addAttribute("list", list);
        model.addAttribute("taskNew", task);
        model.addAttribute("workableList", workableList);
        model.addAttribute("taskSendId", id);
        model.addAttribute("taskSend", taskSend);
        return getView("backList");
    }

    /**
     * 返回反馈页面查看,单位用户,不可操作
     *
     * @param id TaskSend表的主键
     * @return
     */
    @RequestMapping(value = "/backSendList/{id}", method = RequestMethod.GET)
    public String backSendList(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew task = taskNewService.getById(taskId);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(taskId);
        List<TaskBack> list = taskBackService.getBySendId(id);
        model.addAttribute("list", list);
        model.addAttribute("taskNew", task);
        model.addAttribute("workableList", workableList);
        model.addAttribute("taskSendId", id);
        model.addAttribute("taskSend", taskSend);
        //判断当前用户是否是经办单位用户
        String deptId = SysUserUtils.getDeptId();
        SysDept dept = deptService.getById(deptId);
        //如果是县领导,政府办,主官,督查组,返回可编辑的反馈列表
        if (dept.getDeptRelation().equals("/02/")||dept.getDeptRelation().equals("/01/")
                ||dept.getDeptLevel()==1){
            return getView("backList");
        }else {
            return getView("backSendList");
        }
    }

    /**
     * 添加反馈情况
     *
     * @param taskBack
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/feedback", method = RequestMethod.POST)
    public Message feedback(@ModelAttribute TaskBack taskBack) {
        String result = taskBackService.saveBack(taskBack);
        if ("200".equals(result)) {
            return coverMessage("200");
        } else {
            return coverMessage("500");
        }

    }

    /**
     * 详情
     *
     * @param id TaskSend表的主键
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        String taskId = taskSend.getTaskId();
        TaskNew task = taskNewService.getById(taskId);
        List<TaskSend> sendList = taskSendService.queryByTaskId(taskId);
        List<TaskWorkable> workableList = taskWorkableService.queryByTaskId(taskId);
        //抄送人
        List<SysUser> copeToList=new ArrayList<>();
        List<TaskCopeTo> copeTos = taskCopeToService.getByTaskId(id);
        for (TaskCopeTo copeTo : copeTos) {
            SysUser user = sysUserService.getById(copeTo.getCountyLeaderId());
            copeToList.add(user);
        }
        model.addAttribute("taskNew", task);
        model.addAttribute("copeToList", copeToList);//任务抄送人集合
        model.addAttribute("sendList", sendList);
        model.addAttribute("workableList", workableList);
        model.addAttribute("taskSendId", id);
        return getView("detail");
    }


    /**
     * 返回申请撤销,协调任务页面
     *
     * @param model
     * @param id    sendid
     * @return
     */
    @RequestMapping(value = "/repeal/{id}", method = RequestMethod.GET)
    public String repeal(Model model, @PathVariable String id) {
        TaskSend taskSend = taskSendService.getById(id);
        TaskNew taskNew = taskNewService.getById(taskSend.getTaskId());
        model.addAttribute("taskSend", taskSend);
        model.addAttribute("taskNew", taskNew);
        return getView("repeal");
    }

    /**
     * 添加申请撤销消息
     *
     * @param message
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/repeal", method = RequestMethod.POST)
    public Message repeal(@ModelAttribute TaskMessage message) {
        taskSendService.saveRepeal(message);
        return coverMessage("200");
    }

    /**
     * 添加任务办结情况
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/endTaskSend", method = RequestMethod.POST)
    public Message endTaskSend(TaskSendFilter filter) {
        if (StringUtils.isBlank(filter.getType())) {
            return coverMessage("500", "请确定是否办结");
        } else {
            String s = taskSendService.updateSendEnd(filter);
            if ("200".equals(s)) {
                return coverMessage("200", "办结成功");
            } else {
                return coverMessage("500", "办结失败");
            }
        }
    }
}
